using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data;
using System.Data;
using System.Windows.Forms;
namespace DnDDB{
	public class Party{
		// utility class.  Just has some static functions


		// party name with name2's party
        public static bool party_characters(string name, string name2)
        {
            getMySQL get = new getMySQL();
            string command = "SELECT character_name1, character_name2 FROM `Character_grouped_with_Character` WHERE " +
                "(character_name1 = '" + name2 + "' OR character_name2 = '" + name2 + "') AND `end` IS NULL;";
            DataTable table = get.GetMySQLFilledTable(command);
            string name3;
            string dbCommand;
            foreach (DataRow row in table.Rows)
            {
                if (row["character_name2"].ToString() == name2)
                {
                    name3 = row["character_name1"].ToString();
                }
                else
                {
                    name3 = row["character_name2"].ToString();
                }
                dbCommand = "INSERT INTO `Character_grouped_with_Character`" +
                    " (character_name1, character_name2, `start`, `end`) values ('"
                       + name + "', '"
                       + name3 + "', "
                       + "NOW(),"
                       + " NULL );";
                try
                {
                    get.GetMySQLExecuteNonQuery(dbCommand);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    MessageBox.Show("Character creation failed.");
                    return false;
                }
            }

            dbCommand = "INSERT INTO `Character_grouped_with_Character`" +
                " (character_name1, character_name2, `start`, `end`) values ('"
                   + name + "', '"
                   + name2 + "', "
                   + "NOW(),"
                   + " NULL );";
            try
            {
                get.GetMySQLExecuteNonQuery(dbCommand);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                MessageBox.Show("Character creation failed.");
                return false;
            }
            return true;
        }
		
		public static bool unparty(string name1){
            getMySQL get = new getMySQL();
			try {
                get.GetMySQLExecuteNonQuery("CALL unparty('" + name1 + "');");
		    } catch (Exception e) {
			    Console.WriteLine(e);
                MessageBox.Show("Removing character from party failed.");
                return false;
		    }
            return true;
		}
		
		public static List<string> party_members(string name){
			getMySQL get = new getMySQL();
			List<string> toReturn = new List<string>();
            string dbcommand = "SELECT cgc.Character_name1 FROM `Character_grouped_with_Character` cgc WHERE cgc.Character_name2 = '"
             + name + "' AND `end` IS NULL;"; 
			DataTable dt = get.GetMySQLFilledTable(dbcommand);
			foreach (DataRow row in dt.Rows){
                toReturn.Add(row["Character_name1"].ToString());
			}

            dbcommand = "SELECT cgc.Character_name2 FROM `Character_grouped_with_Character` cgc WHERE cgc.Character_name1 = '"
			 + name + "' AND `end` IS NULL;"; 
			dt = get.GetMySQLFilledTable(dbcommand);
			foreach (DataRow row in dt.Rows){
                toReturn.Add(row["Character_name2"].ToString());
			}
			
			return toReturn;
		}
	}
}